//
//  SYCourseTableViewController.m
//  BSHOUYIFANG
//
//  Created by 曹雁彬 on 15/8/17.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYCourseTableViewController.h"
#import "MYRequestManger.h"
#import "SYThirdModel.h"
#import "SYCourseTableViewCell.h"
#import "Header.h"
#import "LORefresh.h"
#import "SYDetailViewController.h"
#import "SYHttpTools.h"
@interface SYCourseTableViewController ()
@property(nonatomic,strong)NSMutableArray *dataSoure;
@property(nonatomic,strong)NSArray *sing;
@property(nonatomic,assign)NSInteger temp;

@end

@implementation SYCourseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"精 品";
    _dataSoure = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tabBarController.tabBar.translucent = NO;
    
    // Uncomment the following line to preserve selection between presentations.
     //self.clearsSelectionOnViewWillAppear = YES;
    //_sing = [NSMutableArray array];
    _sing = @[@"ac8ebcd764ebbbcb2f0ff99862352dab"
              ,@"13141d91a65316ddb97fdd27fb384212"
              ,@"6e0b90e956cd38a42fa2c9055f0f2e6e"
              ,@"a34cc6823844ce32c953801087347cb7"];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerClass:[SYCourseTableViewCell class] forCellReuseIdentifier:@"course"];
     _temp = 1;
    [self postData];
    [self addLorefresh];
}

//上拉加载
- (void)addLorefresh
{
    __weak SYCourseTableViewController *product = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        _temp += 1;
       // NSLog(@"%ld",_temp);
        [product postData];

    }];
    
    __weak NSMutableArray *arr = _dataSoure;
     [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
     [arr removeAllObjects];
     _temp = 1;
     [product postData];
     }];
}
- (void)postData
{
    if (_temp == 4) {
        return;
    }

    [MYRequestManger manager:GET RequestWith:[NSString stringWithFormat:@"http://sdxapp.com/appapi.php?act=shiji_index_data&mquan_id=556&F=android&V=1.2.0&sign=%@&page=%ld&user_id=",_sing[_temp-1],_temp] parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dict[@"data"][@"items"];
        for (NSDictionary *dict1 in arr) {
            SYThirdModel *model = [[SYThirdModel alloc]init];
            [model setValuesForKeysWithDictionary:dict1];
            if ([model.img[0] isEqualToString:@""]) {
                continue;
            }
            [self.dataSoure addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.defaultFooter endRefreshing];
        [self.tableView.defaultHeader endRefreshing];
    } error:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _dataSoure.count;
}
//跳转到购买界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SYDetailViewController *detailVC = [[SYDetailViewController alloc]init];
    SYThirdModel *model = self.dataSoure[indexPath.row];
    detailVC.myID = model.myID;
    detailVC.backupTitle = model.title;
    detailVC.backupImg = model.img[0];
    [self.navigationController pushViewController:detailVC animated:YES];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (SCREEN_HEIGHT-113)/2+20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYCourseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"course" forIndexPath:indexPath];
    cell.model = self.dataSoure[indexPath.row];
    return cell;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.translucent =YES;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
